//
//  LFHomePageViewController.m
//  ServiceBaZhou
//
//  Created by weierda on 2017/6/27.
//  Copyright © 2017年 weierda. All rights reserved.
//

#import "LFHomePageViewController.h"

#import "SearchController.h"

#import "HXSearchBar.h"

#import "ZYTokenManager.h"

@interface LFHomePageViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>

//搜索框
@property (nonatomic,strong) HXSearchBar *searchBar;

//主界面视图
@property (nonatomic,strong) UIImageView *backImage;

//搜索界面视图
@property (nonatomic,strong) UIView *searchView;

//搜索界面的tableView
@property (nonatomic,strong) UITableView *tableView;

//tableView的数据源
@property (nonatomic,strong) NSMutableArray *dataArray;

@property (nonatomic,strong) NSMutableArray *saveArray;

@end

@implementation LFHomePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    
    self.saveArray = [NSMutableArray arrayWithCapacity:0];
    
    [self setupUI];
    
    [self readNSUserDefaults];
    
    NSLog(@"==========%@",_saveArray);
    
    [self setupTableView];
}

- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = YES;
}

-(void)readNSUserDefaults{//取出缓存的数据
    NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults];
    //读取数组NSArray类型的数据
    NSArray * myArray = [userDefaultes arrayForKey:@"myArray"];
    self.saveArray = (NSMutableArray *)myArray;
    
    [_tableView reloadData];
}

- (void)setupUI{
    //背景图片
    _backImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, Width, Height)];
    _backImage.backgroundColor = [UIColor whiteColor];
    _backImage.userInteractionEnabled = YES;
    [self.view addSubview:_backImage];
    
    _backImage.image = [UIImage imageNamed:@"homepageImage"];
    //放大镜图片
    UIImageView *searchImage = [[UIImageView alloc]initWithFrame:CGRectMake(146*RateWidth, 118*RateHeight, 460*RateWidth, 470*RateHeight)];
    searchImage.image = [UIImage imageNamed:@"searchImage"];
    [_backImage addSubview:searchImage];
    
    NSArray *btnTitleArray = @[@"关键字查询",@"企业名称查询",@"企业法人查询",@"企业地址查询"];
    
    for (NSInteger i = 0; i < 4; i ++) {
        UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        searchBtn.frame = CGRectMake(100*RateWidth, 680*RateHeight + 140*RateHeight*i, 550*RateWidth, 100*RateHeight);
        [searchBtn setBackgroundImage:[UIImage imageNamed:@"searchPutin"] forState:UIControlStateNormal];
        searchBtn.tag = 1000 + i;
        [searchBtn addTarget:self action:@selector(searchClick:) forControlEvents:UIControlEventTouchUpInside];
        [searchBtn setTitle:btnTitleArray[i] forState:UIControlStateNormal];
        [searchBtn.titleLabel setFont:[UIFont systemFontOfSize:24.0*RateWidth*2]];
        [_backImage addSubview:searchBtn];
    }
}

- (void)searchClick:(UIButton *)button{
    
    switch (button.tag) {
        case 1000:
            //查询关键字
            _dataArray = [NSMutableArray arrayWithObjects:@"小米科技有限责任公司",@"万科企业股份有限公司",@"北京宝亿嵘影业有限公司",@"大连万达商业地产股份有限公司",@"中新力合股份有限公司",@"上海巨人网络科技有限公司",@"珠海格力集团有限公司",@"厦门美图网科技有限公司", nil];
            self.searchBar.placeholder = @"请输入企业名、人名、品牌、地址等关键字";
            break;
            
        case 1001:
            //查询企业名称
            _dataArray = [NSMutableArray arrayWithObjects:@"小米科技有限责任公司",@"万科企业股份有限公司",@"北京宝亿嵘影业有限公司",@"大连万达商业地产股份有限公司",@"中新力合股份有限公司",@"上海巨人网络科技有限公司",@"珠海格力集团有限公司",@"厦门美图网科技有限公司", nil];
            self.searchBar.placeholder = @"请输入企业名称关键字";
            break;
            
        case 1002:
            //查询企业高管
            _dataArray = [NSMutableArray arrayWithObjects:@"董明珠",@"任正非",@"杨元庆",@"雷军",@"章泽天",@"刘强东",@"王石",@"王中磊",@"李彦宏",@"马化腾",@"王健林",@"马云",nil];
            self.searchBar.placeholder = @"请输入法人、股东或者高管名称";
            break;
            
        case 1003:
            //查询企业地址
            _dataArray = [NSMutableArray arrayWithObjects:@"小米科技有限责任公司",@"万科企业股份有限公司",@"北京宝亿嵘影业有限公司",@"大连万达商业地产股份有限公司",@"中新力合股份有限公司",@"上海巨人网络科技有限公司",@"珠海格力集团有限公司",@"厦门美图网科技有限公司", nil];
            self.searchBar.placeholder = @"请输入企业名称关键字";
            break;
            
        default:
            break;
    }
    
    [_backImage setAlpha:0];
    
    [_tableView reloadData];
    
    [_tableView setAlpha:1];
    
    [_searchBar setAlpha:1];
    
    [self.searchBar becomeFirstResponder];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    [_tableView setAlpha:0];
    [_searchBar setAlpha:0];
    [_backImage setAlpha:1];
    self.searchBar.text = @"";
    [self.searchBar resignFirstResponder];
}

- (void)setupTableView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height -64 ) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor whiteColor];
    [self.tableView setAlpha:0];
    [self.view addSubview:self.tableView];
    
    self.searchBar = [[HXSearchBar alloc] initWithFrame:CGRectMake(10, 100, self.view.frame.size.width - 20, 35)];
    self.searchBar.backgroundColor = [UIColor colorWithHexString:@"#168ec8"];
    self.searchBar.delegate = self;
    //输入框提示
    self.searchBar.placeholder = @"请输入企业名、人名、品牌、地址等关键字";
    //TextField
    self.searchBar.searchBarTextField.layer.cornerRadius = 4;
    self.searchBar.searchBarTextField.backgroundColor = [UIColor whiteColor];
    
    //去掉取消按钮灰色背景
    self.searchBar.hideSearchBarBackgroundImage = YES;
    
    self.searchBar.frame = CGRectMake(0, 20, Width, 44);
    [self.searchBar setAlpha:0];
    [self.view addSubview:self.searchBar];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if(_saveArray.count == 0){
        return 1;
    }else{
        return 2;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section == 0 && _saveArray.count > 0){
        return _saveArray.count + 2;
    }
    return _dataArray.count + 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 38;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *iden = @"iden";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden];
    }
    if(indexPath.section == 0 && _saveArray.count > 0){
        if(indexPath.row == 0){
            cell.textLabel.text = @"历史搜索";
            cell.textLabel.font = [UIFont systemFontOfSize:13];
            cell.textLabel.textColor = [UIColor colorWithRed:195/255.0 green:194/255.0 blue:198/255.0 alpha:1.0];
            return cell;
        }else if(indexPath.row == _saveArray.count + 1){
            cell.textLabel.text = @"删除历史记录";
            cell.textLabel.font = [UIFont systemFontOfSize:13];
            cell.textLabel.textColor = [UIColor colorWithRed:195/255.0 green:194/255.0 blue:198/255.0 alpha:1.0];
            return cell;
        }else{
            cell.textLabel.text = _saveArray[indexPath.row - 1];
            cell.textLabel.font = [UIFont systemFontOfSize:14];
            cell.textLabel.textColor = [UIColor blackColor];
            return cell;
        }
    }else if(indexPath.row == 0 ){
        cell.textLabel.text = @"热门搜索";
        cell.textLabel.font = [UIFont systemFontOfSize:13];
        cell.textLabel.textColor = [UIColor colorWithRed:195/255.0 green:194/255.0 blue:198/255.0 alpha:1.0];
        return cell;
    }else{
        cell.textLabel.text = _dataArray[indexPath.row - 1];
        cell.textLabel.font = [UIFont systemFontOfSize:14];
        cell.textLabel.textColor = [UIColor blackColor];
        return cell;
    }
}

#pragma mark - UISearchBar Delegate

//已经开始编辑时的回调
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    HXSearchBar *sear = (HXSearchBar *)searchBar;
    //取消按钮
    sear.cancleButton.backgroundColor = [UIColor clearColor];
    [sear.cancleButton setTitle:@"取消" forState:UIControlStateNormal];
    [sear.cancleButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    sear.cancleButton.titleLabel.font = [UIFont systemFontOfSize:14];
}

//编辑文字改变的回调
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    
}

//搜索按钮
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [ZYTokenManager SearchText:searchBar.text];
    [self readNSUserDefaults];
    SearchController *searchVC = [[SearchController alloc]init];
    searchVC.searchText = searchBar.text;
    [self.navigationController pushViewController:searchVC animated:YES];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    SearchController *search = [[SearchController alloc]init];
    
    if(_saveArray.count > 0 && indexPath.section == 0 && indexPath.row == _saveArray.count + 1)
    {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"确定清空历史记录？" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //清空历史数据并刷新视图
            [ZYTokenManager removeAllArray];
            [self readNSUserDefaults];
            
        }];
        [alert addAction:cancelAction];
        [alert addAction:sureAction];
        [self presentViewController:alert animated:YES completion:nil];
    }
    else if (_saveArray.count > 0 && indexPath.row != 0 && indexPath.section == 0) {
        search.searchText = _saveArray[indexPath.row - 1];
        [self.navigationController pushViewController:search animated:YES];
    }else if (_saveArray.count > 0 && indexPath.row != 0 && indexPath.section == 1){
        search.searchText = _dataArray[indexPath.row - 1];
        [self.navigationController pushViewController:search animated:YES];
    }else if (_saveArray.count == 0 && indexPath.row > 0){
        search.searchText = _dataArray[indexPath.row - 1];
        [self.navigationController pushViewController:search animated:YES];
    }
    
    NSLog(@"%@",search.searchText);
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
